package cc.ucanuup.web.base.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cc.ucanuup.common.AppConsts;
import cc.ucanuup.common.support.base.Msg;
import cc.ucanuup.common.support.easyui.view.EasyuiPage;
import cc.ucanuup.service.base.entity.MasterCode;
import cc.ucanuup.service.base.inter.MasterCodeService;
import cc.ucanuup.utils.StringUtil;
import cc.ucanuup.web.base.model.MasterCodeModel;
import cc.ucanuup.web.base.model.MasterCodeSunModel;
import cc.ucanuup.web.base.model.SearchSunMasterCodeModel;
import cc.ucanuup.web.base.model.SearchTopMasterCodeModel;

/**
 * 文件名： com.bozhon.web.base.controller.MasterCodeController.java
 * 作者：   WenBin
 * 日期：   2017年10月7日
 * 功能说明：常用编码控制层  数据字典
 * =========================================================
 * 修改记录：
 * 修改作者    日期      修改内容
 * =========================================================
 *  Copyright (c) 2010-2011 .All rights reserved.
 */
@Controller
@RequestMapping("/srm/masterCode")
public class MasterCodeController {

	/**
	 * 关键字服务类
	 */
	@Resource
	private MasterCodeService masterCodeService;

	/**
	 * @Method: com.bozhon.web.base.controller.MasterCodeController.modelPage
	 * @Description: 数据字典  主页面
	 * @author: WenBin
	 * @date: 2017年10月26日
	 * @version: 1.0
	 * @return
	 * String
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@PreAuthorize("hasAuthority('srm:masterCode:masterCodePage')")//有TEST权限的才能访问
	@RequestMapping("/masterCodePage")
	//@RequiresPermissions(value = "masterCode:masterCodePage")
	public String modelPage(){
		return "base/power/mastercode/mastercodePage";
	}


	/**
	 * @Method: com.bozhon.web.base.controller.MasterCodeController.createOrUpdateMasterCode
	 * @Description: 创建或者更新资源信息页面
	 * @author: WenBin
	 * @date: 2017年10月27日
	 * @version: 1.0
	 * @param id
	 * @param request
	 * @return
	 * String
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@RequestMapping("/createOrUpdateMasterCodePage")
	//@RequiresPermissions(value = "masterCode:createOrUpdateMasterCodePage")
	public String createOrUpdateMasterCode(String id ,HttpServletRequest request){
		MasterCode masterCode = masterCodeService.findOne(id==null ? "" : id);
		if(masterCode == null){
			masterCode = new MasterCode();
			masterCode.setDicCode(masterCodeService.findMaxCode());
		}
		request.setAttribute("masterCode", masterCode);
		return "base/power/mastercode/createOrUpdateMasterCode";
	}

	/**
	 * @Method: com.bozhon.web.base.controller.MasterCodeController.createSunMasterCodePage
	 * @Description: 创建或者更新子 字典信息
	 * @author: WenBin
	 * @date: 2017年10月27日
	 * @version: 1.0
	 * @param id
	 * @param fid
	 * @param request
	 * @return
	 * String
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@RequestMapping("/createOrUpdateSunMasterCodePage")
	//@RequiresPermissions(value = "masterCode:createOrUpdateSunMasterCodePage")
	public String createSunMasterCodePage(String id ,String fid ,HttpServletRequest request){
		MasterCode masterCode = masterCodeService.findOne(id==null ? "" : id);
		if(masterCode == null){
			masterCode = new MasterCode();
			if(!StringUtil.isEmpty(fid)){
				masterCode.setFatherId(fid);
			}
			masterCode.setDicCode(masterCodeService.findMaxCode());
		}
		request.setAttribute("masterCode", masterCode);
		return "base/power/mastercode/createSunMasterCode";
	}

	/**
	 * @Method: com.bozhon.web.base.controller.MasterCodeController.queryTopMasterCodes
	 * @Description: 查询父级菜单信息
	 * @author: WenBin
	 * @date: 2017年10月27日
	 * @version: 1.0
	 * @param bzpage
	 * @return
	 * @throws Exception
	 * Object
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@RequestMapping("/queryTopMasterCodes")
	@ResponseBody
	public Object queryTopMasterCodes(SearchTopMasterCodeModel bzpage) throws Exception{
		Page<MasterCode> page = masterCodeService.queryTopMasterCodes(bzpage);
		return new EasyuiPage<MasterCode>(page.getContent(), page.getTotalElements(), page.getNumber()+1);
	}

	/**
	 * @Method: com.bozhon.web.base.controller.MasterCodeController.querySunMasterCodes
	 * @Description: 查询子菜单信息
	 * @author: WenBin
	 * @date: 2017年10月27日
	 * @version: 1.0
	 * @param bzpage
	 * @return
	 * @throws Exception
	 * Object
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@RequestMapping("/querySunMasterCodes")
	@ResponseBody
	public Object querySunMasterCodes(SearchSunMasterCodeModel bzpage) throws Exception{
		Page<MasterCode> page = masterCodeService.querySunMasterCodes(bzpage);
		return new EasyuiPage<MasterCode>(page.getContent(), page.getTotalElements(), page.getNumber()+1);
	}

	/**
	 * @Method: com.bozhon.web.base.controller.ModelController.saveOrUpdateRole
	 * @Description: 保存或更新 数据字典
	 * @author: WenBin
	 * @date: 2017年10月26日
	 * @version: 1.0
	 * @param systemMoModel
	 * @param bindResult
	 * @param model
	 * @return
	 * @throws Exception
	 * Object
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@RequestMapping("/saveOrUpdateMasterCode")
	@ResponseBody
	//@Bzlog(method="saveOrUpdateMasterCode",type={OpType.CREATE,OpType.UPDATE},clazz={MasterCodeModel.class})
	public Object saveOrUpdateMasterCode(@Valid MasterCodeModel masterCodeMoModel,BindingResult bindResult, Model model) throws Exception{
		if (bindResult.hasErrors()) {
			List<ObjectError> map =bindResult.getAllErrors();
			return new Msg(false,"50",map);
		}
		MasterCode masterCode = masterCodeService.findOne(masterCodeMoModel.getId() == null? "" :masterCodeMoModel.getId());
		if(masterCode == null){
			masterCode = new MasterCode();
			masterCode.setDicCode(Integer.valueOf(masterCodeMoModel.getDicCode()));
			masterCode.setDicDesc(masterCodeMoModel.getDicDesc());
			masterCode.setRemark(masterCodeMoModel.getRemark());
			masterCode.setFatherId("");
			masterCode.setDicLevel(0);
			masterCode.setDeleted(AppConsts.DELETED_EXIST);
			masterCode.setStatus(AppConsts.STATUS_UNACTIVE);
		}else{
			masterCode.setDicCode(Integer.valueOf(masterCodeMoModel.getDicCode()));
			masterCode.setDicDesc(masterCodeMoModel.getDicDesc());
			masterCode.setRemark(masterCodeMoModel.getRemark());
		}
		masterCode = masterCodeService.save(masterCode);
		return new Msg();
	}

	/**
	 * @Method: com.bozhon.web.base.controller.ModelController.saveOrUpdateRole
	 * @Description: 保存或更新 数据字典 子菜单信息
	 * @author: WenBin
	 * @date: 2017年10月26日
	 * @version: 1.0
	 * @param systemMoModel
	 * @param bindResult
	 * @param model
	 * @return
	 * @throws Exception
	 * Object
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@RequestMapping("/saveOrUpdateSunMasterCode")
	@ResponseBody
	//@Bzlog(method="saveOrUpdateSunMasterCode",type={OpType.CREATE,OpType.UPDATE},clazz={MasterCodeSunModel.class})
	public Object saveOrUpdateSunMasterCode(@Valid MasterCodeSunModel masterCodeMoModel,BindingResult bindResult, Model model) throws Exception{
		if (bindResult.hasErrors()) {
			List<ObjectError> map =bindResult.getAllErrors();
			return new Msg(false,"50",map);
		}
		MasterCode masterCode = masterCodeService.findOne(masterCodeMoModel.getId() == null? "" :masterCodeMoModel.getId());
		if(masterCode == null){
			masterCode = new MasterCode();
			masterCode.setDicCode(Integer.valueOf(masterCodeMoModel.getDicCode()));
			masterCode.setDicDesc(masterCodeMoModel.getDicDesc());
			masterCode.setDicKey(masterCodeMoModel.getDicKey());
			masterCode.setFatherId(masterCodeMoModel.getFatherId());
			masterCode.setRemark(masterCodeMoModel.getRemark());
			masterCode.setDicLevel(1);
			masterCode.setDeleted(AppConsts.DELETED_EXIST);
			masterCode.setStatus(AppConsts.STATUS_UNACTIVE);
		}else{
			masterCode.setDicCode(Integer.valueOf(masterCodeMoModel.getDicCode()));
			masterCode.setDicDesc(masterCodeMoModel.getDicDesc());
			masterCode.setDicKey(masterCodeMoModel.getDicKey());
			masterCode.setFatherId(masterCodeMoModel.getFatherId());
			masterCode.setRemark(masterCodeMoModel.getRemark());
		}
		masterCode = masterCodeService.save(masterCode);
		return new Msg();
	}

	/**
	 * @Method: com.bozhon.web.base.controller.ModelController.deleteSystemModel
	 * @Description: 删除数据字典
	 * @author: WenBin
	 * @date: 2017年10月26日
	 * @version: 1.0
	 * @param id
	 * @return
	 * Object
	 * @throws Exception
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@RequestMapping(value="/deleteMasterCode")
	@ResponseBody
	public Object deleteMasterCode(String id) throws Exception{
		MasterCode masterCode = masterCodeService.findOne(id == null? "" :id);
		if(masterCode!=null){
			masterCode.setDeleted(AppConsts.DELETED_REMOVE);
			masterCode = masterCodeService.save(masterCode);
		}else{
			return new Msg(false,"50","不存在该角色！");
		}
		return new Msg();
	}

	/**
	 * @Method: com.bozhon.web.base.controller.ModelController.updateSystemModelStatus
	 * @Description: 更新数据字典状态
	 * @author: WenBin
	 * @date: 2017年10月26日
	 * @version: 1.0
	 * @param id
	 * @return
	 * @throws Exception
	 * Object
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@RequestMapping(value="/updateMasterCodeStatus")
	@ResponseBody
	public Object updateMasterCodeStatus(String id) throws Exception{
		MasterCode masterCode = masterCodeService.findOne(id == null? "" :id);
		if(masterCode!=null){
			if(masterCode.getStatus() == AppConsts.STATUS_ACTIVE){
				masterCode.setStatus(AppConsts.STATUS_UNACTIVE);
			}else{
				masterCode.setStatus(AppConsts.STATUS_ACTIVE);
			}
			masterCode = masterCodeService.save(masterCode);
		}else{
			return new Msg(false,"50","不存在该角色！");
		}
		return new Msg();
	}
}
